package com.example.myfirstapplication;
//创建数据库
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.db.MyDataBaseOpenHelper;
import com.example.entiy.Student;
import com.example.utilDao.StudentDao;

import java.util.List;

import static com.example.myfirstapplication.R.id.lv_lv1;
import static com.example.myfirstapplication.R.id.view_offset_helper;

public class SqlLiteActivity extends AppCompatActivity {
    private EditText et_sql1;
    private RadioGroup rg_sql1;
    private ListView lv_lv1;

    private MyAdapter adapter = null;
   // private LinearLayout ll_sql1;
    //获取数据库工具类
    StudentDao dao = new StudentDao(SqlLiteActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_lite);
        //从视图中获取控件
        et_sql1 = findViewById(R.id.et_sql1);
        rg_sql1 = findViewById(R.id.rg_sql);
        lv_lv1 = findViewById(R.id.lv_lv1);
        //ll_sql1 = findViewById(R.id.ll_sql1);



        /* 调用数据库帮助类 */
        //这一句代码执行完毕是不会创建数据库的
        MyDataBaseOpenHelper helper = new MyDataBaseOpenHelper(SqlLiteActivity.this);
        //下面这句话执行了次啊会创建数据库
        helper.getWritableDatabase();



        studentView();

    }

    //保存数据按钮   的点击事件
    public void save(View view) {
        // 从视图下面获取输入框中的文本内容
        String name = et_sql1.getText().toString().trim();
        if(TextUtils.isEmpty(name)){             //如果为空,则退出
            Toast.makeText(this,"请输入学生姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        //不为空,进行接下来的操作
        //从按钮组中获取 被 点击的 按钮的序号   可能是系统分配的一个值  而且每一个选项对应唯一的一个id;
        int id = rg_sql1.getCheckedRadioButtonId();
        System.out.println(id);
        String sex ="male";
        if(id == R.id.rb_sql1){
            //男
            sex = "male";
        }else{
            //女
            sex = "female";
        }
        //将获取的数据保存在数据库中
        dao.studentAdd(name,sex);
        Toast.makeText(this,"学生添加成功", Toast.LENGTH_SHORT).show();
        studentView();

    }

    private void studentView() {
        List<Student> students = dao.studentQueryAll();    //获取全部的学生;
        if(adapter == null) {
            adapter = new MyAdapter(students);
            lv_lv1.setAdapter(adapter);
            System.out.println("设置适配器");
        }else{
            //通知适配器更新数据,而不是new新的数据来
            adapter.notifyDataSetChanged();
            System.out.println("更新数据");
        }
    }



    private class MyAdapter extends BaseAdapter{
        List<Student> students =null;

        public MyAdapter(List<Student> students) {
            this.students = students;
        }
//一共有多少条数据
        @Override
        public int getCount() {
            return students.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
        // 界面优化2,使用xml的方式来设置布局(打气筒方式)
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View view = null;
            if(convertView ==null) {
                //将xml文件转换为view对象   inflate 打气(给气球)
                view = View.inflate(SqlLiteActivity.this, R.layout.layout_item, null);
            }else{
                view = convertView;
            }
            //获取xml中的控件
            ImageView iv_sex = view.findViewById(R.id.iv_sex);
            TextView tv = view.findViewById(R.id.tv_name);
            ImageView iv_shanchu = view.findViewById(R.id.iv_shanchu);
            //获取学生
            Student student = students.get(position);
            String sex = student.getSex();
            final String name = student.getName();
            if("male".equals(sex)){
                iv_sex.setImageResource(R.drawable.nan);
            }else{
                iv_sex.setImageResource(R.drawable.nv);
            }
            tv.setText(name);
            //为删除设置一个点击事件
            iv_shanchu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String log = "删除"+ name+ "成功";
                    StudentDao dao = new StudentDao(SqlLiteActivity.this);
                    dao.studentDelete(name);
                    Toast.makeText(SqlLiteActivity.this, log, Toast.LENGTH_SHORT).show();

                    //更新ui界面
                    studentView();
                }
            });


            return view;
        }




/*        // 界面优化1,使用纯代码的方式来设置布局
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            LinearLayout ll = new LinearLayout(SqlLiteActivity.this); //声明布局控件
            ll.setGravity(Gravity.CENTER_VERTICAL);//设置里面的内容居中
            ll.setOrientation(LinearLayout.HORIZONTAL); //水平布局
            ImageView iv = new ImageView(SqlLiteActivity.this);//声明图片控件;
            String sex = students.get(position).getSex(); //获取学生性别
            if("male".equals(sex)){ //如果是男性,显示男性图标
                iv.setImageResource(R.drawable.nan);
            }else{//如果是男]女性,显示女性图标
                iv.setImageResource(R.drawable.nv);
            }
            TextView tv = new TextView(SqlLiteActivity.this); //获取姓名
            tv.setText(students.get(position).getName());
            ll.addView(iv,200,200);//设置图片宽高
            ll.addView(tv);
            return ll;
        }*/






/*        // 数据的具体处理position那一条数据       convertView回收的数据
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv =null;
            if(convertView ==null) {
                System.out.println("缓存view不存在,new一个"+position);
                tv = new TextView(SqlLiteActivity.this);
            }else{
                System.out.println("缓存view存在,复用view"+convertView);
                tv = (TextView) convertView;

            }
            tv.setText(students.get(position).toString());
            return tv;
        }
        */


    }



/*
    //每次调用刷新  获取的全部学生数据显示在页面上
    public void studentView(){
        ll_sql1.removeAllViews();//将目前所有已经加入的控件全部删除
        List<Student> students= dao.studentQueryAll(); //获取全部学生
        for(Student student : students){
            //申明文本控件 , 并加入student中的内容到文本中
            TextView tv = new TextView(SqlLiteActivity.this);
            tv.setText(student.toString());
            //将文本控件加入到线性布局中
            ll_sql1.addView(tv);
        }
    }
*/


}
